<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of newForum
 *
 * @author ivan
 */
include_once 'function.php';


class modifyUser {
    //put your code here
    
    
    
    private $role;
    private $isBanned;
    private $Db;
    private $email;
    private $modified;
   
   
            
    
    
    
    public function __construct($role,$isBanned,$email) {
        
        
        $role = htmlspecialchars($role);
        $isBanned = htmlspecialchars($isBanned);
        $email = htmlspecialchars($email);
       
        
        $this->role = $role;
        $this->isBanned = $isBanned;
        $this->email = $email;
       
        $this->Db = Db();
        
    }
    
    
    public function check(){
        
       
            
            if($this->role == "none" && $this->isBanned == "none"){
                
               return 0;
                
            }
           
            else{
                
                if($this->role != "none" && $this->isBanned != "none"){
                    
                    return 1;//role and status changed
                    
                }
                else if($this->role != "none" && $this->isBanned == "none"){
                    
                    return 2;//only role changed
                }
                else{
                    return 3;//only status changed
                }
                
            }
            
        
        
    }
    
    
    
    
    public function modify(){
        
        
        $this->modified = $this->check();
        
        if($this->modified == 0){
            
            $error = "Nothing was selected";
            return $error;
        }
        else if($this->modified == 1){
            
             $query = $this->Db->prepare('update user SET role = :role, isBanned = :isBanned, dateUpdated = CURDATE() where email = :email');
            $query->execute(array(
            'role' => $this->role,
            'isBanned' => $this->isBanned,
            'email' => $this->email,
            ));
            
            return "ok";
        }
        else if($this->modified == 2){
            
             $query = $this->Db->prepare('update user SET role = :role, dateUpdated = CURDATE() where email = :email');
            $query->execute(array(
            'role' => $this->role,
            'email' => $this->email,
            ));
            
            return "ok";
        }
        else if($this->modified == 3){
            
             $query = $this->Db->prepare('update user SET isBanned = :isBanned, dateUpdated = CURDATE() where email = :email');
            $query->execute(array(
            'email' => $this->email,
            'isBanned' => $this->isBanned,
            ));
            
            return "ok";
        }
            
        
        
        
        
    }
    
    
    public function add(){
        
        
         
         
          $query = $this->Db->prepare('SELECT * FROM topic where topicTitle = :topicTitle');
            $query->execute(array(
            'topicTitle' => $this->topicTitle,
        ));
            
           $response = $query->fetch();
           
           $this->topicID = $response['tid'];
         
         
         
         //Inserting the reply into the reply table
         $query = $this->Db->prepare('INSERT INTO reply(contentOfReply,dateCreated,dateUpdated,topicID,owner) values(:contentOfReply,NOW(),NOW(),:topicID,:owner)');
         $query->execute(array('contentOfReply' => $this->contentOfReply,'topicID' => $this->topicID,'owner' => $_SESSION['id']));
         
         //updating the number of replies in the topic table
          $query = $this->Db->prepare('update topic SET numberOfReplies = numberOfReplies + 1,lastUpdatedBy = :lastUpdatedBy, dateUpdated = NOW() where tid = :topicID');
        
         $query->execute(array(
             'lastUpdatedBy' => $_SESSION['id'],
             'topicID' => $this->topicID,     
        ));
         
         
         //extracting the forum associated to the reply to update its number of replies
         
         $query = $this->Db->prepare('SELECT *from topic where topicTitle = :topicTitle');
         $query->execute(array(
            'topicTitle' => $this->topicTitle,
        ));
         
         $response = $query->fetch();
         
         $this->forumId1 = $response['forumID'];
         
         //updating the number of replies in the forum associated to the topic      
        $query = $this->Db->prepare('update forum SET numberOfReplies = numberOfReplies + 1,lastUpdatedBy = :lastUpdatedBy, dateCreated = NOW() where fid = :forumId1');
        
         $query->execute(array(
             'lastUpdatedBy' => $_SESSION['id'],
             'forumId1' => $this->forumId1,     
        ));
         
         
       
        return 1;
    }
}
